package user

import (
	"5title_gin/global"
	"5title_gin/pkg/redis_ser"
	"5title_gin/pkg/utils/jwts"
	"5title_gin/pkg/utils/res"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// LogoutView 登出
// @Tags 用户管理
// @Summary 登出
// @Description 登出
// @Router /api/logout [post]
// @Produce json
// @Success 200 {object} res.Response{}
func (UserApi) LogoutView(c *gin.Context) {
	token := c.GetHeader("Authorization")[7:]
	zap.S().Warn("token:", token)
	if global.Redis == nil {
		res.OkWithMsg("注销成功", c)
		return
	}
	claims, err := jwts.CheckToken(token)
	if err != nil {
		res.Error("解析 Refresh Token 失败", err, c)
		return
	}

	msg := redis_ser.Logout(token, claims.Claims.UserID)
	if msg != "" {
		res.FailWithMsg(msg, c)
		return
	}
	res.OkWithMsg("注销成功", c)

}
